﻿using NPoco;
using YYBlog.Core.Entity;

namespace YYBlog.Core.Commands.Accounts
{
    public class ChangeProfileCommand
    {
        public string AuthorId { get; set; }

        public string NewEmail { get; set; }

        public string NewDisplayName { get; set; }
    }

    public class ChangeProfileCommandInvoker : ICommandInvoker<ChangeProfileCommand, CommandResult>
    {
        private readonly Database _db;

        public ChangeProfileCommandInvoker(Database db)
        {
            _db = db;
        }

        public CommandResult Execute(ChangeProfileCommand command)
        {
            var author = _db.SingleOrDefaultById<Author>(command.AuthorId);
            if (author == null)
                return new CommandResult("用户信息不存在");

            var snapshot = _db.StartSnapshot(author);

            author.DisplayName = command.NewDisplayName;
            author.Email = command.NewEmail;

            _db.Update(author,snapshot.UpdatedColumns());
            return CommandResult.SuccessResult;
        }
    }
}